package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.maincardmanager;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.MemberInfoBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.ConverterHelper;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.StringUtils;

import java.math.BigDecimal;

import butterknife.BindView;

/**
 * 解绑实体卡
 */
public class PinCardFragment extends BaseFragment {
    public static PinCardFragment newInstance(String uuid) {
        PinCardFragment pinCardFragment = new PinCardFragment();
        Bundle bundle = new Bundle();
        bundle.putString("uuid", uuid);
        pinCardFragment.setArguments(bundle);
        return pinCardFragment;
    }

    @BindView(R.id.tv_xiao_card)
    TextView tvXiaoCard;
    @BindView(R.id.tv_member_name)
    TextView tvMemberName;
    @BindView(R.id.tv_memeber_ye)
    TextView tvMemberYue;
    @BindView(R.id.tv_member_phone)
    TextView tvMemberPhone;
    @BindView(R.id.tv_oils)
    TextView tvOils;

    @Override
    protected int initLayout() {
        return R.layout.fg_pincard;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.PinCard), getActivity());
        tvXiaoCard.setOnClickListener(view1 -> {
            xiaoCardUUID(getArguments().getString("uuid"));
        });
    }

    @Override
    protected void initData(Context mContext) {

    }

    @Override
    protected void loadMethod() {
        getUUIDMemeberData();
    }


    public void getUUIDMemeberData() {
        RequestUtils.MemberByUuid(getContext(), getArguments().getString("uuid"), DataUtil.getInstance().getCompanyId(), new MyObserver<MemberInfoBean>(context) {
            @Override
            public void onSuccess(MemberInfoBean bean) {
                String memberId = bean.getMemberId();

                //会员姓名
                String realName = bean.getRealName();
                if (StringUtils.isEmpty(realName)) {
                    realName = "暂未设置姓名";
                }

                //会员电话号
                String phone = bean.getPhone();
                if (StringUtils.isEmpty(phone)) {
                    phone = "";
                }

                //该参数只是为了显示当前会员属于什么油品的油品列表
                String oilsType = bean.getProductId();
                if (StringUtils.isEmpty(oilsType)) {
                    oilsType = "";
                }
                String productName = bean.getProductName();

                if (StringUtils.isEmpty(memberId)) {
                    ToastUtils.showToast(getContext(), "此卡暂未开通会员！");
                    getActivity().finish();
                } else {
                    String rechargeBalance = bean.getBalance();
                    if (StringUtils.isEmpty(rechargeBalance)) {
                        rechargeBalance = "0";
                    }
                    setUIData(realName, phone, rechargeBalance, productName);
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(getContext(), errorMsg);
            }
        });
    }

    private void setUIData(String realName, String phone, String rechargeBalance, String productName) {
        if (!StringUtils.isEmpty(realName)) {
            tvMemberName.setText(realName);
        }
        if (!StringUtils.isEmpty(phone)) {
            tvMemberPhone.setText(phone);
        }
        if (!StringUtils.isEmpty(rechargeBalance)) {
            double yue = ConverterHelper.toBigDecimal(rechargeBalance).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            tvMemberYue.setText("¥"+yue);
        }
        if (!StringUtils.isEmpty(productName)) {
            tvOils.setText(productName);
        }
    }


    private void xiaoCardUUID(String uuid) {

        RequestUtils.DeleteCard(getContext(), uuid, new MyObserver<BaseBean>(getContext()) {
            @Override
            public void onSuccess(BaseBean result) {
                startWithPop(PinCardSufFragment.newInstance());
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                if (code == ConfigureFile.code) {
                    startWithPop(PinCardSufFragment.newInstance());
                } else {
                    ToastUtils.showToast(getContext(), errorMsg);
                }

            }
        });

    }
}
